package com.example.memo2.data.database

import androidx.lifecycle.LiveData
import androidx.room.*
import com.example.memo2.data.model.Memo
import java.util.Date

@Dao
interface MemoDao {
    
    @Query("SELECT * FROM memos ORDER BY updatedAt DESC")
    fun getAllMemos(): LiveData<List<Memo>>
    
    @Query("SELECT * FROM memos WHERE id = :id")
    suspend fun getMemoById(id: Long): Memo?
    
    @Query("SELECT * FROM memos WHERE DATE(createdAt/1000, 'unixepoch', 'localtime') = DATE(:date/1000, 'unixepoch', 'localtime') ORDER BY updatedAt DESC")
    fun getMemosByDate(date: Long): LiveData<List<Memo>>
    
    @Query("SELECT * FROM memos WHERE reminderTime IS NOT NULL AND reminderTime > :currentTime ORDER BY reminderTime ASC")
    suspend fun getMemosWithReminders(currentTime: Long): List<Memo>
    
    @Insert
    suspend fun insertMemo(memo: Memo): Long
    
    @Update
    suspend fun updateMemo(memo: Memo)
    
    @Delete
    suspend fun deleteMemo(memo: Memo)
    
    @Query("DELETE FROM memos WHERE id IN (:ids)")
    suspend fun deleteMemosByIds(ids: List<Long>)
    
    @Query("SELECT * FROM memos WHERE title LIKE '%' || :query || '%' OR content LIKE '%' || :query || '%' ORDER BY updatedAt DESC")
    fun searchMemos(query: String): LiveData<List<Memo>>
} 